<?
class ccdsFrontendEdit
	extends ccdsBackendPlugin
{
	private $name = "ccdsFrontendEdit";

	function __construct()
	{
		parent::__construct();

		$this->xajax = ccdsXajax::getInstance();

		$this->xajax->register( XAJAX_CCDS_FUNCTION, "openLogin", $this->name );
		$this->xajax->register( XAJAX_CCDS_FUNCTION, "loginUser", $this->name );
		$this->xajax->register( XAJAX_CCDS_FUNCTION, "initApp", $this->name );
		$this->xajax->register( XAJAX_CCDS_FUNCTION, "initNavi", $this->name );
		$this->xajax->register( XAJAX_CCDS_FUNCTION, "updateNavi", $this->name );


		$this->xajax->register( XAJAX_CCDS_FUNCTION, "getNavi", $this->name );
		$this->xajax->register( XAJAX_CCDS_FUNCTION, "updateNavi", $this->name );
		$this->xajax->register( XAJAX_CCDS_FUNCTION, "changeProjectLanguage", $this->name );

		$this->xajax->register( XAJAX_CCDS_FUNCTION, "logout", $this->name );

		$this->xajax->register( XAJAX_CCDS_FUNCTION, "killSession", $this->name );

	}

	/* ---------------------------------------------------------------------------------------------------------------------------------- */

	function killSession() {
		$objResponse = new xajaxResponse();
		
		$_SESSION['CMS'] = array();
		session_write_close();
		
		return $objResponse;
	}




	/* ---------------------------------------------------------------------------------------------------------------------------------- */
	function loginUser( $aFormValues )
	{
		$objResponse = new ccdsResponse();

		$query = "SELECT * FROM user LEFT OUTER JOIN languages ON user_interface_lang_id = lang_id WHERE user_login_name='".$aFormValues['user_loginname']."'";
		$res = $this->ccdSQL->query( $query );


		if ( $row = $this->ccdSQL->fetch_assoc( $res ) )
		{
			if ( $row['user_del'] == 1 )
			{
				$objResponse->assign( "DivLoginFormError", "innerHTML", "Das Benutzerkonto wurde gesperrt!" );
			}elseif ( $row['user_login_pass'] == md5($aFormValues['user_loginpass']) )
			{
				$this->tools->init_session($row);
				$objResponse->script( "$('#CMS_login_window').xWindow('destroy');" );
				$objResponse->loadcommands( $this->initApp( array ()) );
				$objResponse->script("document.fsd.SetVariable('_src', '/ccds_library/flashplayer/arm.mp3');");
				if (1 == $aFormValues['user_cookielogin']) {
					$cookie_value = MD5(time().$row['user_id']);
					$this->tools->set_cookie_fix_domain("CCDS3_USER", $cookie_value, time()+(180*3600*24),"/",$_SERVER['HTTP_HOST']);
				} else $cookie_value='';
				$this->ccdSQL->query("UPDATE user SET user_cookie='".$cookie_value."' WHERE user_id='".$row['user_id']."' LIMIT 1");

			}else
			{
				$objResponse->script("document.fsd.SetVariable('_src', '/ccds_library/flashplayer/disarm.mp3');");
				$objResponse->assign( "DivLoginFormError","style.display","block");
				$objResponse->assign( "DivLoginFormError", "innerHTML", "Das Passwort ist falsch!" );
			}
		}else
		{
			$objResponse->assign( "DivLoginFormError","style.display","block");
			$objResponse->assign( "DivLoginFormError", "innerHTML", "Benutzer nicht gefunden!" );
		}
		return $objResponse;
	}

	/* ---------------------------------------------------------------------------------------------------------------------------------- */


	function logout() {
	  $_SESSION['CMS']= array();
	  unset($_SESSION['CMS']);
	  $objResponse = new ccdsResponse();
	  $objResponse->clsTabManager->destroy('topTab');

	  $objResponse->script("CMS.clear_tiny_instances();");
	  $objResponse->assign("CMS","innerHTML","");
	  $objResponse->loadcommands($this->openLogin(array()));
		$objResponse->script("document.fsd.SetVariable('_src', '/ccds_library/flashplayer/disarm.mp3');");

		$this->tools->set_cookie_fix_domain("CCDS3_USER", '', time()+(180*3600*24),"/",$_SERVER['HTTP_HOST']);

	  return $objResponse;
	}
	/* ---------------------------------------------------------------------------------------------------------------------------------- */

	function openLogin( $aFormValues = false )
	{
		$objResponse = new ccdsResponse();
		$loginForm = $this->smarty->fetch( $this->config->get( 'TPL_DIR' )."login.tpl" );
		

		$objResponse->script( "
				$('<div/>').attr({id:'CMS_login_window'}).xWindow({
					dialog: true,
					width: 470,
					height: 550,
					modal:true,
					resizable:false,
					draggable:false,
					maximizable:false,
					minimizable:false,
					statusbar:false
					
				});
			window.setTimeout(function() {
			},0);
		" );
		$objResponse->assign( "CMS_login_window", "innerHTML", $loginForm );

		return $objResponse;
	}
	/* ---------------------------------------------------------------------------------------------------------------------------------- */

	function initApp()
	{
		$objResponse = new ccdsResponse();


		$objResponse->includeCSS($this->config->get('LIBRARY_WEB').'jquery/xTree/jquery.xTree.css');
		$objResponse->includeCSS($this->config->get('LIBRARY_WEB').'jquery/plugins/xWindow/jquery.xWindow.css');

		$objResponse->waitForCSS(500);

		
		$objResponse->includeScriptOnce($this->config->get('LIBRARY_WEB').'jquery/jquery.ui.all.js');
		
		$objResponse->waitFor("typeof $.ui == 'object'",2000);

		
		$objResponse->includeScriptOnce($this->config->get('LIBRARY_WEB').'jquery/plugins/xWindow/jquery.xWindow.js');


		$objResponse->includeScriptOnce($this->config->get('LIBRARY_WEB').'jquery/plugins/xWindow/jquery.xWindow.dialog.ext.js');

		$objResponse->includeScriptOnce($this->config->get('LIBRARY_WEB').'jquery/plugins/tablednd/jquery.tablednd.js');
		$objResponse->includeScriptOnce($this->config->get('LIBRARY_WEB').'jquery/xTree/jquery.xTree.js');

		$objResponse->includeScriptOnce($this->config->get('LIBRARY_WEB').'jquery/plugins/filemanager/jquery.typeListener.js');
		$objResponse->includeScriptOnce($this->config->get('LIBRARY_WEB').'jquery/plugins/filemanager/jquery.filemanager.js');
		$objResponse->includeScriptOnce($this->config->get('LIBRARY_WEB').'jquery/plugins/filemanager/templating.js');

		$objResponse->includeScriptOnce($this->config->get('LIBRARY_WEB').'jquery/plugins/tooltip/jquery.tooltip.min.js');
		$objResponse->includeScriptOnce($this->config->get('CCDS_DIR').'/ccds/js/comet.js');


		$objResponse->includeScriptOnce($this->config->get('LIBRARY_WEB').'jquery/plugins/jgcharts/jgcharts.js');


		$objResponse->includeScriptOnce($this->config->get('LIBRARY_WEB').'codemirror/js/codemirror.js');
		$objResponse->waitFor("typeof $.xWindow == 'function'",400);
		$objResponse->waitFor("typeof $.xTree == 'function'",2000);



		//$objResponse->includeCSS($this->config->get('LIBRARY_WEB').'jquery/plugins/filemanager/jquery.filemanager.css');

		//$objResponse->includeCSS($this->config->get('LIBRARY_WEB').'jquery/plugins/tooltip/jquery.tooltip.css');

		if (!$this->check_login() ) {
			$objResponse->loadCommands($this->openLogin());
		}
//
//		
//		$sHtml = $this->smarty->fetch( $this->config->get( 'TPL_DIR' ).'layout.tpl' );
//		$objResponse->assign( "CMS", "innerHTML", $sHtml );
//
//		$objResponse->clsTabManager->create( 'topTab', array
//		(
//			"tabPanel" => 'ContentEditorNavi',
//			"tabContent" => 'ContentArea',
//			"cssClass" => 'topTab',
//			"closeable_all" => true
//		));

//		$objResponse->loadcommands( $this->initNavi() );
	
	return $objResponse;
	}

	/* ---------------------------------------------------------------------------------------------------------------------------------- */
	function initNavi()
	{
		
		$objResponse = new ccdsResponse();
		$projects = array ();

		if ( $_SESSION['CMS']['user_level'] < 1000 )
		{
			$query = "SELECT * FROM
	               projects
	               LEFT JOIN
	               permissions_project_user ON (project_user_key_project_id=project_id)
	               WHERE
	               project_user_key_user_id='".$_SESSION['CMS']['user_id']."'
	               AND
	               project_user_read = 1
	               ORDER by project_name";
		}
		else
		{
			$query = "SELECT * FROM
	               projects
	               WHERE
	               1
	               ORDER by project_name";
		}
		$res = $this->ccdSQL->query( $query );

		$languages=array();
	
		if (!is_array($_SESSION['CMS']['lang'])) $_SESSION['CMS']['lang'] = array();
		while ( $row = $this->ccdSQL->fetch_assoc( $res ) )
		{
			$projects[] = $row;
		}
		$this->smarty->assign( "projects", $projects );
		$sHtml = $this->smarty->fetch( $this->config->get( 'TPL_DIR' )."projects/project_navi_container.tpl" );
		$objResponse->assign( "divprojects", "innerHTML", $sHtml );		

		foreach ($projects as $project) {
			$objResponse->loadcommands( $this->updateProjectNavi($project) );
		}

		return $objResponse;
	}
	/* ---------------------------------------------------------------------------------------------------------------------------------- */

	function getNaviEntries( $aFormValues = array() )
	{
		if ( $project_id != false )
		{
			$project_query = " AND obj1.obj_key_project_id='".$project_id."' ";
		}
		else
		{
			$project_query = "";
		}
		$query = "
			SELECT 
				obj1.*,
				class_name,
				class_dir,
				ext_dir,
				project_dir,
				objp_key_obj_id
			FROM 
					obj_index as obj1  
				STRAIGHT_JOIN classes ON
					(obj1.obj_key_class_id = class_id)
				STRAIGHT_JOIN extensions ON
					(ext_id = class_key_ext_id)
				STRAIGHT_JOIN projects ON
					(project_id = ext_key_project_id)
			LEFT OUTER JOIN 
				obj_index_public ON 
				(objp_key_obj_id=obj_id AND objp_key_lang_id=".$_SESSION['CMS']['lang'][$aFormValues['project_id']].")
			WHERE 
				obj1.obj_parent_obj_id = '".$aFormValues['obj_parent_obj_id']."' 
				AND obj1.obj_deleted = 0 
				".$project_query."
			ORDER BY 
				obj1.obj_position
		";	
		$res = $this->ccdSQL->query($query);
		$results = array();
		while ( $row = $this->ccdSQL->fetch_assoc( $res ) ) 
		{

			if ( $row['obj_link_obj_id'] > 0 )
			{
				$query = "SELECT * FROM obj_index 
										STRAIGHT_JOIN classes ON
											(obj_key_class_id = class_id)
										STRAIGHT_JOIN extensions ON
											(ext_id = class_key_ext_id)
										STRAIGHT_JOIN projects ON
											(project_id = ext_key_project_id)											
										WHERE obj_id='".$row['obj_link_obj_id']."'
										
					";
				$res2 = $this->ccdSQL->query( $query );
				$link_obj = $this->ccdSQL->fetch_assoc( $res2 );
			
				$link_obj['obj_id'] = $row['obj_id'];
			
				$row = $link_obj;
			}
			

			if ( file_exists( $this->config->get( 'EXTENSION_DIR' ).$row['project_dir']."/".$row['ext_dir']."/".$row['class_dir']."/img/icon.png" ) )
			{
				$icon_src = $this->config->get( 'EXTENSION_WEB' ).$row['project_dir']."/".$row['ext_dir']."/".$row['class_dir']."/img/icon.png";
			}
			else
			{
				$icon_src = $this->config->get( 'LIBRARY_WEB' )."famfamfam/icons/page.png";
			}
			$name = $this->tools->getObjectName( $row['obj_id'],$_SESSION['CMS']['lang'][$row['obj_key_project_id']], true );
			$row = array_merge( $row, $name );
			$row['icon'] = $icon_src;

			$query = "SELECT COUNT(*) as anzahl FROM obj_index WHERE obj_parent_obj_id='".$row['obj_id']."' AND obj_deleted='0'";
			$res2 = $this->ccdSQL->query( $query );
			$anzahl = $this->ccdSQL->fetch_assoc( $res2 );
			$count = $anzahl['anzahl'];


			$row['childs'] = $count;
			$results[] = $row;
		}

		return $results;
	}
	/* ---------------------------------------------------------------------------------------------------------------------------------- */

	function getNavi( $aFormValues = array() )
	{
		$objResponse = new ccdsResponse();
		
		
		$results = $this->getNaviEntries( $aFormValues );
		
		
		$elements = array();

		foreach($results as $obj) {

			$disabled = $obj['objp_key_obj_id'] > 0 ? 0 : 1;

			$add_obj = array(
				"id" => "navi_obj_".$obj['obj_id'],
				"name" => $obj['obj_name_name'],
				"icon" => $obj['icon'],
				"disabled" => $disabled,
				"onNameClick" => $this->tools->strip("function() {
					xajax.request(
						{ccdsfun: 'ContentEditorOpen', 
						ccdsclass: 'ccdsContentEditor',
						ccdscomet:1}, {comet:1,parameters: [{obj_id: '".$obj['obj_id']."'}]});
					}"),
				"iconAttr" => array("title" => "id: ".$obj['obj_id']." Klasse: ".$obj['class_name'])
			);
			if ($obj['childs'] > 0) 
			{
				$add_obj['folder'] = true;
				$add_obj['onOpen'] = "function() {xajax.request({ccdsfun: 'getNavi', ccdsclass: 'ccdsFrontendEdit'}, {parameters: [{obj_parent_obj_id: '".$obj['obj_id']."',project_id:".$aFormValues['project_id']."}]});}";
			}
			$elements[] = $add_obj;
		}

		$objResponse->script($this->tools->strip("$('#CMS_project_tree_".$aFormValues['project_id']."').xTree('update',".json_encode(
			array(
				"parent" => array("id" => "navi_obj_".$aFormValues['obj_parent_obj_id']),
				"elements" => $elements
			)
		).")"));

		$objResponse->script("$('#CMS_project_tree_".$aFormValues['project_id']."').xTree('open',{id:'navi_obj_".$aFormValues['obj_parent_obj_id']."'})");
		
		return $objResponse;
	}

	/* ---------------------------------------------------------------------------------------------------------------------------------- */

	function updateNaviEntry( $aFormValues = array() , $childs = false )
	{
		$objResponse = new ccdsResponse();

		if ( "" == $aFormValues['project_id'] ) 
		{
			$query = "SELECT obj_key_project_id FROM obj_index WHERE obj_id='".addslashes($aFormValues['obj_parent_obj_id'])."' LIMIT 1";
			$res = $this->ccdSQL->query( $query );
			$project = $this->ccdSQL->fetch_assoc( $res );
			$aFormValues['project_id'] = $project['obj_key_project_id'];			
		}
		
		if ( 0 == $aFormValues['obj_parent_obj_id']) $aFormValues['parent_id'] = "project_navi_root_".$aFormValues['project_id'];

		if ( $project['project_id'] != false )
		{
			$project_query = " AND obj1.obj_key_project_id='".$project['project_id']."' ";
		}
		else
		{
			$project_query = "";
		}
		$query = "
			SELECT 
				obj1.*,
				class_name,
				class_dir,
				ext_dir,
				project_dir
			FROM 
					obj_index as obj1  
				STRAIGHT_JOIN classes ON
					(obj1.obj_key_class_id = class_id)
				STRAIGHT_JOIN extensions ON
					(ext_id = class_key_ext_id)
				STRAIGHT_JOIN projects ON
					(project_id = ext_key_project_id)
			LEFT OUTER JOIN 
				obj_index_public ON 
				(objp_key_obj_id=obj_id AND objp_key_lang_id=".$_SESSION['CMS']['lang'][$aFormValues['project_id']].")
			WHERE 
				obj1.obj_id = '".$aFormValues['obj_parent_obj_id']."' 
				AND obj1.obj_deleted = 0 
				".$project_query."
			ORDER BY 
				obj1.obj_position
		";	
		
		$res = $this->ccdSQL->query($query);
		$results = array();
		$row = $this->ccdSQL->fetch_assoc( $res );
		if ( $row['obj_link_obj_id'] > 0 )
		{
			$query = "SELECT * FROM obj_index 
									STRAIGHT_JOIN classes ON
										(obj_key_class_id = class_id)
									STRAIGHT_JOIN extensions ON
										(ext_id = class_key_ext_id)
									STRAIGHT_JOIN projects ON
										(project_id = ext_key_project_id)											
									WHERE obj_id='".$row['obj_link_obj_id']."'
									
				";
			$res2 = $this->ccdSQL->query( $query );
			$link_obj = $this->ccdSQL->fetch_assoc( $res2 );
		
			$link_obj['obj_id'] = $row['obj_id'];
		
			$row = $link_obj;
		}
		

		if ( file_exists( $this->config->get( 'EXTENSION_DIR' ).$row['project_dir']."/".$row['ext_dir']."/".$row['class_dir']."/img/icon.png" ) )
		{
			$icon_src = $this->config->get( 'EXTENSION_WEB' ).$row['project_dir']."/".$row['ext_dir']."/".$row['class_dir']."/img/icon.png";
		}
		else
		{
			$icon_src = $this->config->get( 'LIBRARY_WEB' )."famfamfam/icons/page.png";
		}
		$name = $this->tools->getObjectName( $row['obj_id'],$_SESSION['CMS']['lang'][$row['obj_key_project_id']], true );
		$row = array_merge( $row, $name );
		$row['icon'] = $icon_src;

		$query = "SELECT COUNT(*) as anzahl FROM obj_index WHERE obj_parent_obj_id='".$row['obj_id']."' AND obj_deleted='0'";
		$res2 = $this->ccdSQL->query( $query );
		$anzahl = $this->ccdSQL->fetch_assoc( $res2 );
		$count = $anzahl['anzahl'];

		$row['childs'] = $count;

		$update_obj = array(
			"id" => "navi_obj_".$row['obj_id']
			,"name" => $row['obj_name_name']
			//,"icon" => $row['icon']
			//,"onNameClick" => "function() {xajax.request({ccdsfun: 'ContentEditorOpen', ccdsclass: 'ccdsContentEditor'}, {parameters: [{obj_id: '".$row['obj_id']."'}]});}"
			//,"iconAttr" => array("title" => "id: ".$row['obj_id']." Klasse: ".$row['class_name'])
		);
 		

		if (!$childs) 
		{
			$objResponse->script("$('#CMS_project_tree_".$aFormValues['project_id']."').xTree('update',".json_encode(
				array(
					"parent" => $update_obj
				)
			).")");
			return $objResponse;
		}

		$results = $this->getNaviEntries( $aFormValues );
		$elements = array();

		foreach($results as $obj) {
			$disabled = $obj['objp_key_obj_id'] > 0 ? 0 : 1;
			$add_obj = array(
				"id" => "navi_obj_".$obj['obj_id'],
				"name" => $obj['obj_name_name'],
				"icon" => $obj['icon'],
				"onNameClick" => "function() {xajax.request({ccdsfun: 'ContentEditorOpen', ccdsclass: 'ccdsContentEditor'}, {parameters: [{obj_id: '".$obj['obj_id']."'}]});}",
				"iconAttr" => array("title" => "id: ".$obj['obj_id']." Klasse: ".$obj['class_name']),
				"disabled" => $disabled
			);
			if ($obj['childs'] > 0) 
			{
				$add_obj['folder'] = true;
				$add_obj['onOpen'] = "function() {
					xajax.request({ccdsfun: 'getNavi', ccdsclass: 'ccdsFrontendEdit'}, {parameters: [{obj_parent_obj_id: '".$obj['obj_id']."',project_id:".$aFormValues['project_id']."}]});
				}";
			}
			$elements[] = $add_obj;
		}

		$objResponse->script("$('#CMS_project_tree_".$aFormValues['project_id']."').xTree('update',".json_encode(
			array(
				"parent" => $update_obj
				,"elements" => $elements
			)
		).")");



		return $objResponse;
	}
	/* ---------------------------------------------------------------------------------------------------------------------------------- */

	function updateNavi( $aFormValues = array() )
	{
		$objResponse = new ccdsResponse();
		
	//	$objResponse->alert("updateNavi - ohne Funktion");
		
		if ( "" == $aFormValues['project_id'] ) 
		{

			$query = "SELECT obj_key_project_id FROM obj_index WHERE obj_id='".addslashes($aFormValues['obj_parent_obj_id'])."' LIMIT 1";
			$res = $this->ccdSQL->query( $query );
			$project = $this->ccdSQL->fetch_assoc( $res );
			$aFormValues['project_id'] = $project['obj_key_project_id'];			
			
		}
		
		if ( 0 == $aFormValues['obj_parent_obj_id']) $aFormValues['parent_id'] = "project_navi_root_".$aFormValues['project_id'];
		
		
		$results = $this->getNaviEntries( $aFormValues );
		$elements = array();

		foreach($results as $obj) {
			$disabled = $obj['objp_key_obj_id'] > 0 ? 0 : 1;
			$add_obj = array(
				"id" => "navi_obj_".$obj['obj_id'],
				"name" => $obj['obj_name_name'],
				"icon" => $obj['icon'],
				"onNameClick" => "function() {xajax.request({ccdsfun: 'ContentEditorOpen', ccdsclass: 'ccdsContentEditor'}, {parameters: [{obj_id: '".$obj['obj_id']."'}]});}",
				"iconAttr" => array("title" => "id: ".$obj['obj_id']." Klasse: ".$obj['class_name']),
				"disabled" => $disabled
			);
			if ($obj['childs'] > 0) 
			{
				$add_obj['folder'] = true;
				$add_obj['onOpen'] = "function() {
					xajax.request({ccdsfun: 'getNavi', ccdsclass: 'ccdsFrontendEdit'}, {parameters: [{obj_parent_obj_id: '".$obj['obj_id']."',project_id:".$aFormValues['project_id']."}]});
				}";
			}
			$elements[] = $add_obj;
		}

		$objResponse->script($this->tools->strip("$('#CMS_project_tree_".$aFormValues['project_id']."').xTree('update',".json_encode(
			array(
				"parent" => $aFormValues['parent_id'],
				"elements" => $elements
			)
		).")"));

	//	$objResponse->script("$('#CMS_project_tree_".$aFormValues['project_id']."').xTree('open',{id:'navi_obj_".$aFormValues['obj_parent_obj_id']."'})");
		
		

		return $objResponse;
	}



	/* ---------------------------------------------------------------------------------------------------------------------------------- */


	function changeProjectLanguage( $aFormValues ) 
	{
		$objResponse = new ccdsResponse();
		
		$_SESSION['CMS']['lang'][$aFormValues['project_id']] = $aFormValues['lang_id'];
				
		if (is_array($aFormValues['opened']))
		{
			foreach ($aFormValues['opened'] as $opened)
			{
				$matches = array();
				if (1 == preg_match("/^navi_obj_([0-9])/",$opened,$matches))
				{
					
					$objResponse->loadcommands(
						$this->updateNaviEntry(array(
							'project_id' => $aFormValues['project_id'],
							'obj_parent_obj_id' => $matches[1],
							'parent_id' => $opened
						))
					);
				}
			}
		}
		$languages = array();
		$query = "SELECT languages.* FROM languages LEFT JOIN mn_project_language ON   	project_language_key_language_id=lang_id WHERE project_language_key_project_id='".addslashes($aFormValues['project_id'])."'";
		$res = $this->ccdSQL->query($query);
		while ($row = $this->ccdSQL->fetch_assoc($res)) $languages[]=$row;
		$this->smarty->assign("project_id",$aFormValues['project_id']);
		$this->smarty->assign("languages",$languages);
		$sHtml = $this->smarty->fetch( $this->config->get( 'TPL_DIR' )."projects/project_languages.tpl" );
		$objResponse->assign("project_languages_".$aFormValues['project_id'],"innerHTML",$sHtml);
		//$objResponse->script("$('#project_languages_".$aFormValues['project_id']."').toggle()");
		return $objResponse;
	}


	/* ---------------------------------------------------------------------------------------------------------------------------------- */

	function updateProjectNavi( $aFormValues )
	{

		$elements = array();;

		$objResponse = new ccdsResponse();
			
		$query = "SELECT * FROM projects WHERE project_id = '".$aFormValues['project_id']."'";
		$res = $this->ccdSQL->query($query);
		$project = $this->ccdSQL->fetch_assoc($res);
					
		$project['languages'] = array();
		$query = "SELECT * FROM mn_project_language LEFT JOIN languages ON project_language_key_language_id = lang_id WHERE project_language_key_project_id='".$project['project_id']."'";
		$reslang = $this->ccdSQL->query($query);
		while ($lang = $this->ccdSQL->fetch_assoc($reslang)) 
		{
			$project['languages'][] = $lang;
			if ($lang['lang_id'] == $_SESSION['CMS']['user_lang_id']) $_SESSION['CMS']['lang'][$project['project_id']] = $lang['lang_id'];
		}
		
		if ("" == $_SESSION['CMS']['lang'][$project['project_id']])
		{
			$lang = $project['languages'][0];
			$_SESSION['CMS']['lang'][$project['project_id']] = $lang['lang_id'];
		}
		$_SESSION['CMS']['user_lang_id']="";
		
		$query = "SELECT COUNT(*) as anzahl FROM obj_index WHERE obj_deleted=1 AND obj_key_project_id='".$project['project_id']."'";
		$res2 = $this->ccdSQL->query( $query );
		$trash = $this->ccdSQL->fetch_assoc( $res2 );
		
		
		$project['trash'] = $trash['anzahl'];
		
		$query = "
							SELECT 
								* 
							FROM 
								obj_index 

							STRAIGHT_JOIN classes ON
								(obj_key_class_id = class_id)
							STRAIGHT_JOIN extensions ON
								(ext_id = class_key_ext_id)
							STRAIGHT_JOIN projects ON
								(project_id = ext_key_project_id)

							LEFT OUTER JOIN 
								obj_index_public ON 
								(objp_key_obj_id=obj_id AND objp_key_lang_id='".$_SESSION['CMS']['lang'][$aFormValues['project_id']]."')

							WHERE 
								obj_parent_obj_id=0 
								AND obj_deleted=0
								AND obj_key_project_id='".$project['project_id']."'
						";

		
		$cres = $this->ccdSQL->query($query);
		$childs = array();
		while ( $obj = $this->ccdSQL->fetch_assoc( $cres )) {
			$disabled = $obj['objp_key_obj_id'] > 0 ? 0 : 1;
			$name = $this->tools->getObjectName( $obj['obj_id'], $_SESSION['CMS']['lang'][$project['project_id']], true );
			$obj = array_merge( $obj, $name );

			if ( file_exists( $this->config->get( 'EXTENSION_DIR' ).$row['project_dir']."/".$row['ext_dir']."/".$row['class_dir']."/img/icon.png" ) )
			{
				$icon_src = $this->config->get( 'EXTENSION_WEB' ).$row['project_dir']."/".$row['ext_dir']."/".$row['class_dir']."/img/icon.png";
			}
			else
			{
				$icon_src = $this->config->get( 'LIBRARY_WEB' )."famfamfam/icons/house.png";
			}

			$elements[] = array(
				"id" => "navi_obj_".$obj['obj_id'],
				"name" => $obj['obj_name_name'],
				"icon" => $icon_src,
				"onNameClick" => "xajax.request({ccdsfun: 'ContentEditorOpen', ccdsclass: 'ccdsContentEditor'}, {parameters: [{obj_id: '".$obj['obj_id']."'}]});",
				"onOpen" => "function(parent_id) {xajax.request({ccdsfun: 'getNavi', ccdsclass: 'ccdsFrontendEdit'}, {parameters: [{parent_id:parent_id,obj_parent_obj_id: '".$obj['obj_id']."',project_id:".$obj['obj_key_project_id']."}]});}",
				"folder" => true,
				"iconAttr" => array("title" => "id: ".$obj['obj_id']." Klasse: ".$obj['class_name']),
				"disabled" => $disabled
			);
		}
		

		
		if (($_SESSION['CMS']['user_level'] >= 1000) OR ($_SESSION['CMS']['user_flags']['general_trash']))
		{
			$trash = array(
				"id"=>"navi_trash_".$project['project_id'],
				"name" => "Papierkorb (".$project['trash'].")",
				"onNameClick" => "xajax.request({ccdsfun:'TrashOpen',ccdsclass:'ccdsTrash'},{parameters:[{project_id:'".$project['project_id']."'}]});"
			);
			if (0 < $project['trash'])
			{
				$trash['icon'] =  $this->config->get( 'LIBRARY_WEB' )."famfamfam/icons/bin.png";
			}
			else {
				$trash['icon'] =  $this->config->get( 'LIBRARY_WEB' )."famfamfam/icons/bin_closed.png";
			}
			$elements[] = $trash;
		}


		if (($_SESSION['CMS']['user_level'] >= 1000) OR ($_SESSION['CMS']['user_flags']['project_classes']))
		{
			$trash = array(
				"icon" => $this->config->get("LIBRARY_WEB")."/famfamfam/icons/package.png",
				"id"=>"navi_classes_".$project['project_id'],
				"name" => "Extensions / Klassen",
				"onOpen" => "xajax.request({ccdsfun:'ClassEditorExtensionList',ccdsclass:'ccdsClassEditor'},{parameters:[{project_id:'".$project['project_id']."'}]});",
				"onNameClick" => "xajax.request({ccdsfun:'ClassEditorExtensionOverview',ccdsclass:'ccdsClassEditor'},{parameters:[{project_id:'".$project['project_id']."'}]});",
				"folder" => true
			);
			$elements[] = $trash;
		}

		if (($_SESSION['CMS']['user_level'] >= 1000) OR ($_SESSION['CMS']['user_flags']['project_classes']))
		{
			$trash = array(
				"icon" => $this->config->get("LIBRARY_WEB")."/famfamfam/icons/page_white_code.png",
				"id"=>"navi_classes_".$project['project_id'],
				"name" => "Templates",
				"onNameClick" => "xajax.request({ccdsfun:'ClassEditorTemplatesList',ccdsclass:'ccdsClassEditor'},{parameters:[{project_id:'".$project['project_id']."'}]});",
				"folder" => false
			);
			$elements[] = $trash;
		}


		if (($_SESSION['CMS']['user_level'] >= 1000) OR ($_SESSION['CMS']['user_flags']['project_classes']))
		{
			$trash = array(
				"icon" => $this->config->get("LIBRARY_WEB")."/famfamfam/icons/chart_bar.png",
				"id"=>"navi_classes_".$project['project_id'],
				"name" => "Google Analytics",
				"onNameClick" => "xajax.request({ccdsfun:'AnalyticsOpen',ccdsclass:'ccdsAnalytics'},{parameters:[{project_id:'".$project['project_id']."'}]});",
				"folder" => false
			);
			$elements[] = $trash;
		}

		$this->smarty->assign( "project", $project );
		$sHtml = $this->smarty->fetch( $this->config->get( 'TPL_DIR' )."projects/project_navi.tpl" );
		$objResponse->assign( "CMS_project_".$project['project_id'], "innerHTML", $sHtml );
		
				
		$objResponse->script("$('#CMS_project_tree_".$project['project_id']."').xTree(".json_encode(array("animate" => false,"multipleSelect" => false, "elements" => $elements)).")");


		return $objResponse;
	}

	function check_login() {
		
		if ( is_numeric($_SESSION['CMS']['user_id']) && $_SESSION['CMS']['user_id'] > 0 ) return true;
		
//		if ( isset($_COOKIE['CCDS3_USER']{10}) && !empty($_COOKIE['CCDS3_USER']) ) 
//		{
//			$query = "SELECT * FROM `user` WHERE user_cookie='".$_COOKIE['CCDS3_USER']."'";
//			$res = $this->ccdSQL->query($query);
//			if (!$row = $this->ccdSQL->fetch_assoc($res)) return false;
//
//			$this->tools->init_session($row);
//			return true;
//		}
		
		
		return false;
		
	}


	/* ---------------------------------------------------------------------------------------------------------------------------------- */

	function run()
	{
		if ($this->check_login())
		{
			$onloadScript = "xajax.request({ccdsfun:'initApp',ccdsclass:'ccdsFrontendEdit'},{parameters:[{}]});";
		}
		else
		{
			$onloadScript = "xajax.request({ccdsfun:'openLogin',ccdsclass:'ccdsFrontendEdit'},{parameters:[{}]});";
		}
		$xajax = ccdsXajax::getInstance();
		$xajax->configure( "javascript URI", $this->config->get( 'LIBRARY_WEB' )."xajax/" );

		$xajax->registerFunction( "customFunction" );
		$xajax->registerFunction( "customFormFunction" );

		$this->smarty->assign( 'xajax_javascript', $xajax->getJavascript() );

		$this->smarty->assign( "onLoadScript", $onloadScript );
		$this->smarty->display( $this->config->get( 'TPL_DIR' )."index.tpl", "" );
	}
/* ---------------------------------------------------------------------------------------------------------------------------------- */

}
